﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using App.Data;
using App.Core.Extensions;

public partial class Pages_Admin_AdvertisementList : BasePage
{
    private const int PAGE_SIZE = 20;
    private bool _IsDeleteMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDeleteData();
            BindAdvertisementList(1);
        }
    }
    protected void BindPageInfo()
    {
        _IsDeleteMode = String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0 ? true : false;
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void CheckAndDeleteData()
    {
        if (_IsDeleteMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            int id = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
            var advertisement = context.Advertisements.FirstOrDefault(A => A.ID == id);
            if (advertisement == null)
                WebUtil.ShowMessageBox(divMessage, "Requested Advertisement was not found for delete.", true);
            else
            {
                advertisement.IsDeleted = true;
                advertisement.Modified = DateTime.Now;
                advertisement.ModifiedBy = SessionCache.CurrentUser.ID;
                context.SaveChanges();
                WebUtil.ShowMessageBox(divMessage, "Advertisement Deleted Successfully.", false);
            }
        }
    }
    protected void BindAdvertisementList(int pageNumber)
    {
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        DataSet ds = dao.GetPagedData(AppSQL.PAGED_ADVERTISEMENT_LIST_FOR_ADMIN, null, pageNumber, PAGE_SIZE, out totalRecord);
        ucAdvertisementList.DataSource = ds.Tables[0];
        ucAdvertisementList.EditLink = "/Pages/Admin/AdvertisementChange.aspx?ID={0}";
        ucAdvertisementList.DeleteLink = Request.Url.AbsolutePath + "?ID={0}&" + AppConstants.QueryString.DELETE + "=True";
        ucAdvertisementList.DataBind();

        ///Bind Pager Control
        ucPagerBottom.TotalRecord = totalRecord;
        ucPagerBottom.PageSize = PAGE_SIZE;
        ucPagerBottom.PageNo = pageNumber;
        ucPagerBottom.TotalRecordMessage = "Total {0} Advertisement(s) found.";
        ucPagerBottom.DataBind();
    }
    protected void ucPagerBottom_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindAdvertisementList(e.PageIndex);
    }
}